package com.welove.http;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.util.EntityUtils;

public class HttpResponseImpl implements IHttpResponse {


	
	private HttpResponse response;
	private HttpEntity entity;
	
	public HttpResponseImpl(HttpResponse response) throws IOException
	{
		
		this.response = response;
		HttpEntity tempEntity = response.getEntity();
		if(null !=tempEntity)
		{
			entity = new BufferedHttpEntity(tempEntity);
		}
		
		
	}
	
	
	public int statusCode() {
	
		return response.getStatusLine().getStatusCode();
	}

	@Override
	public InputStream getResponseBody() throws IllegalStateException, IOException {
		
		return entity.getContent();
	}

	@Override
	public byte[] getResponseBodyAsByte() throws IOException {
		// TODO Auto-generated method stub
		return EntityUtils.toByteArray(entity);
	}

	@Override
	public String getResponseBodyAsString() throws ParseException, IOException {
		// TODO Auto-generated method stub
		return EntityUtils.toString(entity);
	}

}
